package br.mikhas.util.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Implementation of {@link ResultSetExtrator} which maps a single column and
 * single row result into a simple java type.
 * 
 * @author n068815 - Mikhail Domanoski
 * 
 */
class SingleValueResultSetExtractor implements ResultSetExtrator<Object> {

	/**
	 * The required java type
	 */
	private Class<?> requiredType;

	/**
	 * Default constructor which the desired java type should be providen.
	 * 
	 * @param type
	 *            The desired Java type.
	 */
	SingleValueResultSetExtractor(Class<?> type) {
		this.requiredType = type;
	}

	/**
	 * Extracts a single object from the resultset which is from the desired
	 * type.
	 */
	public Object extractData(ResultSet rs) throws JdbcException {
		try {
			rs.next();
		} catch (SQLException e) {
			throw new JdbcException("Error while moving cursor.", e);
		}
		return JdbcUtils.getResultSetValue(rs, 1, this.requiredType);
	}

}
